﻿package mortal.game.view.vip.panel
{
    import Message.DB.Tables.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.player.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class VipExperienceWin extends Window
    {
        private var _bg:GImageBitmap;
        private var _reChargeBtn:GLoadingButton;
        private var _tipTxt:GTextFiled;
        public var itemData:ItemData;
        private var _rect3d:Rect3DObject;
        private var _img2d:Img2D;
        private var _bodyPlayer:WizardActionPlayer;
        private var timerContorler:TimeControler;
        private var _isOverTime:Boolean;
        private static var _instance:VipExperienceWin;

        public function VipExperienceWin()
        {
            if (_instance != null)
            {
                throw new Error(" VipExperienceWin 单例 ");
            }
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.setSize(720, 320);
            this.timerContorler = TimeControler.createTimerInstance();
            this.timerContorler.unActive();
            titleHeight = 80;
            this._rect3d = Rect3DManager.instance.getRect3dByWindow(this);
            if (!this._rect3d)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(0, 0, 716, 394), this);
            }
            var _loc_1:* = GlobalStyle.textFormatHuang2;
            _loc_1.size = 20;
            _loc_1.bold = true;
            this._tipTxt = UIFactory.gTextField("", 553, 58, 150, 30, this.contentTopOf3DSprite, _loc_1);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._tipTxt.dispose(param1);
            this._tipTxt = null;
            if (this._bg)
            {
                this._bg.dispose(param1);
                this._bg = null;
            }
            if (this._reChargeBtn)
            {
                this._reChargeBtn.dispose(param1);
                this._reChargeBtn = null;
            }
            if (this._img2d)
            {
                this._img2d.dispose(param1);
                this._img2d = null;
            }
            if (this._bodyPlayer)
            {
                this.timerContorler.unActive();
                FrEventDispatcher.instance.proxyRemoveEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._rect3d.disposeObj3d(this._bodyPlayer);
                this._bodyPlayer = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            this.itemData = null;
            this._isOverTime = false;
            super.disposeImpl(param1);
            return;
        }// end function

        protected function onWindowShow(event:DataEvent) : void
        {
            if (event.data != this)
            {
                this.hide();
            }
            return;
        }// end function

        private function add2d() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this._img2d = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 736, 400), true);
            this._rect3d.addImg(this._img2d);
            this.add3d();
            return;
        }// end function

        private function add3d() : void
        {
            var _loc_1:* = ModelConfig.instance.getInfoByCode(int(WizardConfig.instance.getSpriteByCodeAndLevel(2, 1).modelId));
            var _loc_2:* = _loc_1.mesh1 + ".md5mesh";
            var _loc_3:* = _loc_1.bone1 + ".skeleton";
            if (this._bodyPlayer == null)
            {
                this._bodyPlayer = FrObjectPool.getObject(WizardActionPlayer, this);
                FrEventDispatcher.instance.proxyAddEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.timerContorler = this.timerContorler;
            }
            this.timerContorler.active();
            this._bodyPlayer.clearExtend();
            this._bodyPlayer.load(_loc_2, _loc_3, _loc_1.texture1, this._rect3d.renderList);
            if (_loc_1.mesh2)
            {
                _loc_2 = _loc_1.mesh2 + ".md5mesh";
                _loc_3 = _loc_1.bone2 + ".skeleton";
                this._bodyPlayer.loadExtend(_loc_2, _loc_3, _loc_1.texture2, this._rect3d.renderList);
                this._bodyPlayer.extendPlayer.timerContorler = this.timerContorler;
            }
            this._bodyPlayer.play();
            this._bodyPlayer.scaleValue = 3;
            this._bodyPlayer.rotationY = 0;
            this._rect3d.addObject3d(this._bodyPlayer, 0, 370, false);
            this._bodyPlayer.changeAction(ActionName.Standing);
            return;
        }// end function

        protected function onInitBodyPlayer(event:Event) : void
        {
            if (this._bodyPlayer)
            {
                this._bodyPlayer.addFrameComplete(ActionName.Standing, this.onFrameComplete);
            }
            return;
        }// end function

        private function onFrameComplete() : void
        {
            if (!this._bodyPlayer)
            {
                return;
            }
            this.setActionName(ActionName.Stand);
            return;
        }// end function

        private function useVipCard() : void
        {
            var _loc_1:* = Cache.instance.pack.backPackCache.getItemByCode(this.itemData.itemCode, false);
            if (_loc_1)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Use, _loc_1));
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(35584));
            }
            return;
        }// end function

        private function rechargeMoney(event:MouseEvent) : void
        {
            GameUtil.gotoRecharge(ModuleTypeConst.VipExperence);
            return;
        }// end function

        public function setActionName(param1:String) : void
        {
            this._bodyPlayer.changeAction(param1);
            return;
        }// end function

        public function updateState(param1:Boolean = false) : void
        {
            this._isOverTime = param1;
            if (!param1)
            {
                this._reChargeBtn = UIFactory.gLoadingButton(ResFileConst.VipExperienceGetBtn, 300, 338, 221, 44, this.contentTopOf3DSprite);
                this._reChargeBtn.configEventListener(MouseEvent.CLICK, this.closeWin);
                this._bg = UIFactory.gImageBitmap(ImagesConst.vipExperience_get, 0, 0, null, this.add2d);
                this._tipTxt.text = "30       00";
            }
            else
            {
                this._reChargeBtn = UIFactory.gLoadingButton(ResFileConst.VipExperienceRenewalBtn, 300, 338, 221, 44, this.contentTopOf3DSprite);
                this._reChargeBtn.configEventListener(MouseEvent.CLICK, this.rechargeMoney);
                this._bg = UIFactory.gImageBitmap(ImagesConst.vipExperience_over, 0, 0, null, this.add2d);
                this._tipTxt.text = Language.getString(35585);
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            LayerManager.windowLayer.removeAllWindow();
            LayerManager.windowLayer3D.removeAllWindow();
            super.show();
            return;
        }// end function

        private function closeWin(event:MouseEvent) : void
        {
            this.hide();
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            if (this._isOverTime == false)
            {
                this.useVipCard();
            }
            return;
        }// end function

        public static function get instance() : VipExperienceWin
        {
            if (_instance == null)
            {
                _instance = new VipExperienceWin;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
